package com.cx.controller;

import com.cx.pojo.Area;
import com.cx.pojo.City;
import com.cx.pojo.Province;
import com.cx.service.AreaService;
import com.cx.service.CityService;
import com.cx.service.ProvinceService;
import com.cx.service.impl.AreaServiceImpl;
import com.cx.service.impl.CityServiceImpl;
import com.cx.service.impl.ProvinceServiceImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class ReactinChainServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("UTF-8");

        String action=request.getParameter("action");
        Gson gson=new Gson();
        if("province".equals(action)){
            ProvinceService provinceService=new ProvinceServiceImpl();
            List<Province> provinceList=provinceService.selectAll();


            String provinceJson=gson.toJson(provinceList);
            response.getWriter().write(provinceJson);
        }else if("city".equals(action)){
            String proId=request.getParameter("proid");
            CityService cityService=new CityServiceImpl();
            List<City> cities=cityService.selectByProId(proId);
            String cityJson=gson.toJson(cities);
            response.getWriter().write(cityJson);
        }else if("area".equals(action)){
            String cityId=request.getParameter("cityid");
            AreaService areaService=new AreaServiceImpl();
            List<Area> areas=areaService.selectByCityId(cityId);
            String provinceJson=gson.toJson(areas);
            response.getWriter().write(provinceJson);
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request,response);
    }
}
